var CARD = {
		textSuccess: '',
		textError: '',
		val_bk: 0,
		init: function(){
			$("ul.tabs").tabs("div.panes > div");
			$('ul.tabs > li').click(function(){
				$('div.tab-active').text($('a',$(this)).html());
			});
			CARD.change();
			CARD.period_add();
			CARD.period_change();
			CARD.changeCardPhoto();
		},
		changeCardPhoto:function(){
			$('img.upload-preview-card').uploadControl({
				file_type: 'jpg|jpeg|png',
				path: 'all',
				language: 'vi',
				max_depth: 0,
				buttons:'upload|select|remove|rename|reload|mkdir|language',
				callback:{
					beforeSelect: function(selectedFile, currentControl){},
					afterSelected: function(selectedFile, currentControl){},
					onClosed: function(selectedFile, currentControl){
						if(selectedFile){
							$.ajax({
								type: "POST",
								url: '/admin/bankings/bankingToCardColumnsUpdatePhoto',
								data: {
									val: ADMIN.uploadPath+selectedFile.path,
									banking_id: currentControl.attr('data-bank'),
									banking_card_id: currentControl.attr('data-card')
								},
								cache: false,
								success: function(data){
									if(parseInt(data, 10) > 0){
										$('div.save-result').text(CARD.textSuccess);
										currentControl.attr('src',ADMIN.uploadPath+selectedFile.path);
										if(!currentControl.parent().parent().attr('data-bank')){
											currentControl.parent().parent().attr('data-bank',data);
										};
										$('input.period-text',currentControl.parent().parent()).each(function(){
											if(!$(this).attr('data-bank')){
												$(this).attr('data-bank',data);
											}
										});
									}else{
										$('div.save-result').text(CARD.textError);
									}
								}
							});
						}
					}
				}
			});
		},
		change:function(){
			$('input.period-text').click(function(){
				if($(this).attr('readonly')=='readonly'){
					$(this).removeAttr('readonly');
					$(this).removeClass('none')
					$(this).addClass('active');
					CARD.val_bk = $(this).val();
					$('div.save-result').text('');
				}
			}).blur(function(){
				if($(this).attr('readonly')!='readonly'){
					$(this).attr('readonly', 'readonly');
					$(this).removeClass('active')
					$(this).addClass('none');
					if($(this).val()==''){
						//$(this).val('0');
					}
					if($(this).val()!=CARD.val_bk){
						CARD.update($(this));
					}
				}
			});
			
		},
		update: function(obj){
			$('div.save-result').text('');
			$.ajax({
				type: "POST",
				url: '/admin/bankings/bankingToCardColumnsUpdate',
				data: {
					val: obj.val(),
					banking_id: obj.attr('data-bank'),
					banking_card_column_id: obj.attr('data-card')
				},
				cache: false,
				success: function(data){
					if(parseInt(data, 10) > 0){
						$('div.save-result').text(CARD.textSuccess);
					}else{
						$('div.save-result').text(CARD.textError);
					}
				}
			});
		},
		//HEAD
		period_change:function(){
			$('input.period-change').click(function(){
				if($(this).attr('readonly')=='readonly'){
					$(this).removeAttr('readonly');
					$(this).removeClass('none')
					$(this).addClass('active');
					CARD.val_bk = $(this).val();
					$('div.save-result').text('');
				}
			});
			$('input.period-change').blur(function(){
				if($(this).attr('readonly')!='readonly'){
					$(this).attr('readonly', 'readonly');
					$(this).removeClass('active')
					$(this).addClass('none');
					if($(this).val()==''){
						$(this).val(CARD.val_bk);
					}
					if($(this).val()!=CARD.val_bk){
						CARD.period_update($(this));
					}
					
				}
			});
		},
		period_update: function(obj){
			$('div.save-result').text('');
			$.ajax({
				type: "POST",
				url: '/admin/bankings/bankingToCardColumnsInsert',
				data: {
					name: obj.val(),
					id: obj.attr('data-id')
				},
				cache: false,
				success: function(data){
					if(parseInt(data, 10) > 0){
						$('div.save-result').text(CARD.textSuccess);
					}else{
						$('div.save-result').text(CARD.textError);
					}
				}
			});
		},
		
		period_add:function(){
			$('input.period-add').click(function(){
				if($(this).attr('readonly')=='readonly'){
					$(this).val('');
					$(this).removeAttr('readonly');
					$(this).removeClass('none')
					$(this).addClass('active');
					$('div.save-result').text('');
				}
			}).blur(function(){
				if($(this).attr('readonly')!='readonly'){
					$(this).attr('readonly', 'readonly');
					$(this).removeClass('active')
					$(this).addClass('none');
					if($(this).val()==''){
						$(this).val('+');
					}else{
						CARD.period_insert($(this));
					}
					
				}
			});
		},
		period_insert: function(obj){
			$('div.save-result').text('');
			$.ajax({
				type: "POST",
				url: '/admin/bankings/bankingToCardColumnsInsert',
				data: {
					name: obj.val(),
					card_id: obj.attr('data-cur'),
					parent_id: obj.attr('data-card')
				},
				cache: false,
				success: function(data){
					if(parseInt(data, 10) > 0){
						//$('input.period-add').each(function(){
							var data_card = parseInt(obj.attr('data-card'), 10);
							if(data_card > 0){
								var head_td = $('<td class="head"></td>');
								var head_indput = $('<input type="text" data-card="'+data_card+'" data-cur="'+obj.attr('data-cur')+'" class="period-add none" readonly="true" name="period_add" value="+"/>');
								head_td.append(head_indput);
								obj.parent().parent().append(head_td);
								var head_cur = $('<input type="text" class="period-change none" data-id="'+data+'" readonly="true" name="period_add" value="'+$(obj).val()+'"/>');
								obj.parent().attr('class','head name').append(head_cur);
								obj.remove();
								$('tr.tr-name', $('table.tbl-'+obj.attr('data-cur'))).each(function(){
									var input = $('<input type="text" data-cur="'+$(this).attr('data-cur')+'" data-bank="'+$(this).attr('data-bank')+'" data-card="'+data+'" class="period-text none" readonly="true" name="period" value=""/>');
									var td_last = $('table.child-'+obj.attr('data-card')+' td.child-last', $(this));
									if(td_last.html()=='&nbsp;'){
										td_last.html('');
										td_last.append(input);
										td_last.removeClass('child-last');
										td_last.addClass('name');
										var td_new = $('<td class="child-last"></td>');
										td_new.html('&nbsp;');
										td_last.parent().append(td_new);
									}else{ 
										var oThis = $(this);
										$('table.child-'+obj.attr('data-card')+' tr td', $(this)).each(function(){
											$(this).html('');
											var input = $('<input type="text" data-cur="'+oThis.attr('data-cur')+'" data-bank="'+oThis.attr('data-bank')+'" data-card="'+data+'" class="period-text none" readonly="true" name="period" value=""/>');
											$(this).append(input);
											var td_new = $('<td class="child-last"></td>');
											td_new.html('&nbsp;');
											$(this).parent().append(td_new);
										});
										
									}
									
									
								});
							}else{
								var head_td = $('<td class="head"></td>');
								var head_indput = $('<input type="text" data-cur="'+obj.attr('data-cur')+'" class="period-add none" readonly="true" name="period_add" value="+"/>');
								head_td.append(head_indput);
								obj.parent().parent().append(head_td);
								var head_cur = $('<input type="text" class="period-change none" data-id="'+data+'" readonly="true" name="period_add" value="'+$(obj).val()+'"/>');
								obj.parent().append(head_cur);
								var strTbl = '<table class="table-list" style="background: none; width:100%;border-bottom:none;border-left:none;border-right:none"><tr><td class="head"><input type="text" data-cur="'+obj.attr('data-cur')+'" class="period-add none" data-card="'+data+'" readonly="true" name="period_add" value="+"/></td></tr></table>';
								obj.parent().append(strTbl);
								obj.remove();
								$('tr.tr-name', $('table.tbl-'+obj.attr('data-cur'))).each(function(){
									var input = $('<table class="table-list child-'+data+'" style="background: none; width:100%;border-bottom:none;border-left:none;border-right:none"><tr><td class="name"><input type="text" data-cur="'+$(this).attr('data-cur')+'" data-bank="'+$(this).attr('data-bank')+'" data-card="'+data+'" class="period-text none" readonly="true" name="period" value=""/></td></tr></table>');
									var td_last = $('td.last', $(this));
									td_last.html('');
									td_last.append(input);
									td_last.removeClass('last');
									var td_new = $('<td class="name last"></td>');
									td_new.html('&nbsp;');
									$(this).append(td_new);
									
								});
							}
							
						//});
						
						CARD.change();
						CARD.period_add();
						CARD.period_change();
						$('div.save-result').text(CARD.textSuccess);
					}else{
						$('div.save-result').text(CARD.textError);
					}
				}
			});
		}
		//END HEAD
		
}